/* globals sessionStorage */
import http from './base/axios'

export default {
	login(data) {
		return http.post('/login', data).then(resp => {
			console.log(resp);
			sessionStorage.setItem('token', resp.data.access_token);
			sessionStorage.setItem('refreshToken', resp.data.refresh_token);
		});
	},

	loginByQQ(data) {
		return http.post('/login/qq', data).then(resp => {
			console.debug("set loggedInUser", resp);
			localStorage.setItem('loggedInUser', JSON.stringify(resp));
			localStorage.setItem("loggedIn", true);
		});
	},

	getToken() {
		return localStorage.token;
	},

	getRefreshToken() {
		return localStorage.refreshToken;
	},

	logout() {
		localStorage.removeItem("loggedIn");
		localStorage.removeItem("accessToken");
	},

	loggedIn() {
		console.debug("loggedIn()",localStorage.getItem("loggedIn"))
		if (undefined == localStorage.getItem("loggedIn")) {
			return false;
		}
		return true;
	},

	getByAccessToken() {
		return http.get('/user/getByAccessToken').then(resp => resp.data);
	},

	getUser() {
		return JSON.parse(localStorage.getItem('loggedInUser'));
	},

	getUserId() {
		return JSON.parse(localStorage.getItem('loggedInUser')).id;
	},

	onChange() {}
}
